<template>
  <div>
    <h1>广告列表</h1>
    <el-table :data="items">
      <el-table-column prop="_id" label="ID" width="230"></el-table-column>
      <el-table-column prop="title" label="广告名称"></el-table-column>
      <el-table-column fixed="right" label="操作" width="180">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="$router.push(`/ads/edit/${scope.row._id}`)"
            >编辑</el-button
          >
          <el-button type="text" size="small" @click="remove(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <my-pagination
      :result="result"
      v-if="result.length"
      @getItems="($event) => (items = $event)"
    ></my-pagination>
  </div>
</template>

<script>
export default {
  data () {
    return {
      result: [], // 传递给子组件处理的值
      items: []
    }
  },
  created () {
    // 放在这里自动执行
    this.fetch()
  },
  methods: {
    async fetch () {
      const res = await this.$http.get('rest/ads')
      this.result = res.data.items
    },
    async remove (row) {
      this.$confirm(`是否删除广告-${row.title}`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        // 请求删除的接口
        const res = await this.$http.delete(`rest/ads/${row._id}`)
        console.log(res)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.fetch()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  },
  components: {

  }
}
</script>

<style scoped>
</style>
